home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.07 Jul 95 / 11.07 Challenge Article next >
Encoding:
Text File  |  1995-06-05  |  3.7 KB  |  36 lines  |  [TEXT/R*ch]

  1. Sprite Blitz
  2. Most of you are probably familiar with the great shareware arcade games that are available for the Mac.  This month you will be writing a key element of an arcade game – the code that moves graphic elements (“sprites”) across a background.  There are a number of libraries that do much of this work for the aspiring game writer, but I want to see how fast you can do it, and hopefully we will all benefit from the techniques in the winning code.
  3. The prototypes of the code you must write are as follows:
  4.  
  5. void StartGame(
  6. /* pointer to CWindow */
  7.  CWindowPtr theWind
  8. );
  9.  
  10. short /*theSpriteID*/ AddSprite(
  11. /* pointer to ‘cicn' */
  12.     CIconPtr theSprite,
  13. /* initial sprite location */
  14.     Point startLoc 
  15. );    /* returns sprite ID to be used in MoveSprite, DeleteSprite */
  16.  
  17. void DeleteSprite(
  18. /* ID of sprite to delete */
  19.     short theSpriteID 
  20. );
  21.  
  22. void MoveSprite(
  23. /* ID of sprite to move */
  24.     short theSpriteID,            
  25. /* amount to move sprite 
  26.     NOTE: offset, not new location */
  27.     Point amountToMove            
  28. );
  29.  
  30. void UpdateScreen(void);    
  31.  
  32. The problem works like this.  Your routine StartGame will be called first and will be provided with a pointer to a color window.  The window will be preset to a background that must be preserved, except for portions that are obscured by sprites.  StartGame will not be timed, so you can do whatever initialization you need to do, like allocating an offscreen pixmap and initializing it to the contents of the window.  Your routines AddSprite, DeleteSprite, and MoveSprite will then be called repeatedly to add, delete, and move sprites.  Your routine UpdateScreen will also be called repeatedly, at which time you must redraw all sprites at their current locations and restore the background at their old locations.
  33. Sprites will be provided to AddSprite as ‘cicn’ data structures, described in IM V-80 or NIM Imaging 4-105.  Even though a ‘cicn’ can have an arbitrary shape, the sprites your code sees will have widths that are 1, 2, 4, 8, 16, or 32 pixels, to simplify things, but any height up to 32 pixels.  A ‘cicn’ also has an associated mask, and you need to remember to show the correct part of the background through any holes in the mask.  One way to draw the sprites, of course, is with the toolbox routine PlotCIcon, but you might find a better way…
  34. To simplify the problem, you can rely on (**theWind->portPixMap).bounds being longword aligned on a single monitor that is in 256 color mode, and on the pixelSize being 8 bits.  The background will not change over time (i.e., you don’t have to handle a scrolling background).  You can assume that there will be no more than 200 active sprites, and you can assume a sprite will not move more than 8 pixels horizontally or more than 8 pixels vertically in any given MoveSprite call.  The color table of each sprite ‘cicn’ will be the same as that in the window PixMap.  In a real game, you would probably do some collision detection on sprites, but our sprites will pass thru one another oblivious to the collision.  In the event sprites overlap, you should draw them in the order that they were created by AddSprite (so that the sprite created last would be drawn over other sprites).  You will need to do bounds checking of the sprite location against the window, because our sprites may move partially or completely out of the window.  
  35. This problem would be appropriate for an assembly language Challenge, but we’ll see how well you can do in straight C.  Entries with obviously jerky visual effects will be considered less “correct” than those with smooth visual effects.  I’ll select the winner from the correct entries based on how fast the code runs on a 68040 using the THINK C compiler, but – deadline permitting – I’ll also measure native execution time on a PowerPC.  Now start blitting those sprites!
  36.